package com.xsm.activitygroup;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.http.message.BasicNameValuePair;

import com.umeng.analytics.MobclickAgent;
import com.xsm.DataBase.SaveMessage;
import com.xsm.activitygroup.R;
import com.xsm.internet.PostMessage;
import com.xsm.view.LoadDialog;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class EditMessage extends Activity{
private EditText editText;
private	String content;
private	EDHandler handler;
private	String type,value;
private	String user;
private	String result;
private Context context;
private	LoadDialog dialog;
private	Button edit;
private String title;
private TextView titTextView;
private int USERNAME=1,PHONE=2,SIGNATURE=3;
private InputMethodManager inputManager;
private TextView maxtext;
private int MAXTEXT=20;

protected void onCreate(Bundle savedInstanceState) {
	requestWindowFeature(Window.FEATURE_NO_TITLE);
	super.onCreate(savedInstanceState);
	setContentView(R.layout.editmessage);
	type=getIntent().getExtras().getString("type");
	value=getIntent().getExtras().getString("value");
	editText=(EditText)findViewById(R.id.content);
	titTextView=(TextView)findViewById(R.id.t1);
	maxtext=(TextView)findViewById(R.id.maxtext);
	maxtext.setText(MAXTEXT+"");
	edit=(Button)findViewById(R.id.edit);
	edit.getBackground().setAlpha(180);

	
	if(type.equals("issuephone")){
	editText.setInputType(InputType.TYPE_CLASS_NUMBER);
	}else if(type.equals("signature")){
	maxtext.setVisibility(View.VISIBLE);
	}
	String t1=getResources().getString(R.string.rewrite);
    title=t1+getTitleString(type); 
    
	editText.addTextChangedListener(new TextWatcher() {
		public void onTextChanged(CharSequence s, int start, int before, int count) {
		}
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}
		public void afterTextChanged(Editable s){
		    String afterString=editText.getText().toString().trim();
			if(value.equals(afterString)){
			edit.setEnabled(false);
			}else {
			edit.setEnabled(true);
			}
			if(type.equals("signature")||type.equals("name")){
			int leng=MAXTEXT-afterString.length();
			if(leng>=0){
				maxtext.setText(leng+"");
			}else{
				editText.setText(afterString.substring(0,MAXTEXT));
			}
			}
		}
	});
	
	titTextView.setText(title);
	
	editText.setHint(title.substring(2, title.length()));
	editText.setText(value);
	editText.setSelection(value.length());
	
	editText.setFocusable(true);
    editText.setFocusableInTouchMode(true);
    editText.requestFocus();

    Timer timer = new Timer();
     timer.schedule(new TimerTask()
     {
         public void run() 
         {
       inputManager = (InputMethodManager)editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
       inputManager.showSoftInput(editText, 0);
         }
      },  
     300);
	
     
	handler=new EDHandler();
	context=this;
	SharedPreferences sharedPreferences=getSharedPreferences("XSM",0);
	user=sharedPreferences.getString("User","");
	dialog=new LoadDialog(context,"正在提交...");
}


private void closeSoftInput(){
	if(inputManager!=null)
	if (inputManager.isActive()) { 
		//如果开启 
		inputManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS); 
		//关闭软键盘，开启方法相同，这个方法是切换开启与关闭状态的 
		inputManager=null;
	} 
}

private String getTitleString(String type){
	int id=0;
	if(type.equals("issuephone")){
		id=PHONE;
	}else if(type.equals("name")){
		id=USERNAME;
	}else if(type.equals("signature")){
		id=SIGNATURE;
	}
String t2="";
switch (id) {
case 1:
t2=getResources().getString(R.string.Username);
break;
case 2:
t2=getResources().getString(R.string.Phone);
break;
case 3:
t2=getResources().getString(R.string.Signature);
default:
break;
}
return t2;
}

public void Edit(View view){
dialog.showDialog();
if(getWindow().getAttributes().softInputMode==WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED){
	getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
	}
	new Thread(new Runnable() {
		public void run() {
			String editvalue=editText.getText().toString().trim();
			List params=new ArrayList();
			params.add(new BasicNameValuePair("ASK","UpdateUserMessage"));
			params.add(new BasicNameValuePair("user",user));
			params.add(new BasicNameValuePair("value",editText.getText().toString().trim()));
			params.add(new BasicNameValuePair("type",type));
			PostMessage postMessage=new PostMessage(params);
			result=postMessage.getResult();
			Message msgMessage=handler.obtainMessage();
			if(result.equals("-1")){
				msgMessage.what=-1;
			}else if(result.equals("1")){
				msgMessage.what=1;
			new SaveMessage(context, user).UpdateMessage(user, type,editvalue);	
			}else if(result.equals("2")){
				msgMessage.what=2;
			}
			handler.sendMessage(msgMessage);
		}
	}).start();
}


public void Back(View view){
	Intent intent=new Intent();
	intent.putExtra("type", type);
	intent.putExtra("value", value);
	setResult(RESULT_OK, intent);
	int flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; 
	getWindow().addFlags(flags); 
	finish();
}

protected void onDestroy() {
super.onDestroy();
}


public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK){
	int flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; 
	getWindow().addFlags(flags); 
	Intent intent=new Intent();
	intent.putExtra("type", type);
	intent.putExtra("value", value);
	setResult(RESULT_OK, intent);
	finish();
}
	return super.onKeyDown(keyCode, event);
}

class EDHandler extends Handler{
public void handleMessage(Message msg) {
	dialog.dissmissDialog();
if(msg.what==-1){
	Toast.makeText(context,R.string.neterror, Toast.LENGTH_LONG).show();
}else if(msg.what==2){
	Toast.makeText(context,getTitleString(type)+getString(R.string.zhanyong), Toast.LENGTH_LONG).show();
}else if (msg.what==1){
	value=editText.getText().toString().trim();
	Intent intent=new Intent();
	intent.putExtra("type", type);
	intent.putExtra("value", value);
	setResult(RESULT_OK, intent);
	Toast.makeText(context,R.string.UpdateSuccess, Toast.LENGTH_LONG).show();	
	finish();
}
}
}


protected void onResume() {
	MobclickAgent.onResume(this);
	super.onResume();
}
protected void onPause() {
	MobclickAgent.onResume(this);
	super.onPause();
}
}
